package com.huanghj.base.hessianAnnotations.builder;


import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.remoting.caucho.HessianProxyFactoryBean;
import org.springframework.stereotype.Controller;

import com.huanghj.base.hessianAnnotations.HessianClient;
import com.huanghj.base.hessianAnnotations.util.PropertiesUtil;




/**
 * 自动加载Hessian所需要的bean  
 * cenxi  2016-12-17
 * */
public class HessianClientBuilder implements ApplicationContextAware  {

	

	public void setApplicationContext(ApplicationContext applicationContext)throws BeansException {
		System.out.println("HessianClientBuilder start......");
		try {
			//获取soa地址
			Map<String, String> urlMap = PropertiesUtil.readProperties(PropertiesUtil.COMMONBASE);
			
			System.out.println(urlMap);
			
			// 将applicationContext转换为ConfigurableApplicationContext
			ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) applicationContext;

			// 获取bean工厂并转换为DefaultListableBeanFactory
			DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) configurableApplicationContext.getBeanFactory();
			
			String[] beanNames = applicationContext.getBeanDefinitionNames();
			for(String beanName : beanNames){
				Class clazz = applicationContext.getType(beanName);
				// 判断是否是Remote
				Annotation controllerAnnotation = clazz.getAnnotation(Controller.class);
				//controllerAnnotation != null
				if (true) {
					Field[] fields = clazz.getDeclaredFields();
					for (java.lang.reflect.Field f : fields) {
						HessianClient hessianClientAnnotation = f.getAnnotation(HessianClient.class);
						if(hessianClientAnnotation != null){
							String urlType = hessianClientAnnotation.value();
							String interfacePackage = f.getType().getName();
							String simpleName = f.getType().getSimpleName();
							//首字母小写
							String first = simpleName.substring(0, 1).toLowerCase();
							String rest = simpleName.substring(1, simpleName.length());
							simpleName = new StringBuffer(first).append(rest).toString();
							
							// 通过BeanDefinitionBuilder创建bean定义
							BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(HessianProxyFactoryBean.class);
							AbstractBeanDefinition beanProxyDefinition = beanDefinitionBuilder.getBeanDefinition(); 
							// 设置属性
		                    MutablePropertyValues propertyValues = new MutablePropertyValues();  
		                    propertyValues.addPropertyValue("serviceUrl", urlMap.get(urlType)+"/annotationHessian/"+simpleName);  
		                    propertyValues.addPropertyValue("serviceInterface", interfacePackage);  
		                    beanProxyDefinition.setPropertyValues(propertyValues); 
							// 注册bean   这一步是很关键的，如果beanFactory不是注册bean的 class 而是注册  bean的定义definition 
							defaultListableBeanFactory.registerBeanDefinition(simpleName,beanProxyDefinition);
							System.out.println("create HessianClient："+simpleName);
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
